#include"TextureManager.h"

bool TextureManager::IsTexture( unsigned int id)
{
	return this->textures.count(id) == 1;
}

bool TextureManager::IsTextureSection( unsigned int id)
{
	return this->texturesections.count(id) == 1;
}

bool TextureManager::IsAnimation( unsigned int id)
{
	return this->animations.count(id) == 1;
}

TextureManager::TextureManager()
{
	this->animations.clear();
	this->textures.clear();
	this->texturesections.clear();
	this->idcounter = 0;
}

TextureManager::~TextureManager()
{
	this->animations.clear();
	this->textures.clear();
	this->texturesections.clear();
	this->idcounter = 0;
}

unsigned int TextureManager::AddTexture(std::string filename)
{
	Texture* texture = new Texture();
	if( ! texture )
		return 0;
	if( texture->LoadTexture(filename) )
		this->textures[++(this->idcounter)] = texture;
	else
		return 0;
	return this->idcounter;
}

unsigned int TextureManager::AddTexture(std::string filename, unsigned char red_alpha, unsigned char green_alpha, unsigned blue_alpha )
{
	Texture* texture = new Texture();
	if( ! texture )
		return 0;
	if( texture->LoadTexture(filename, red_alpha, green_alpha, blue_alpha) )
		this->textures[++(this->idcounter)] = texture;
	else
		return 0;
	return this->idcounter;
}

unsigned int TextureManager::AddTextureSection(unsigned int TextureId)
{
	if( this->textures.count( TextureId ) > 0 )
	{
		TextureSection* texturesection = new TextureSection();
		if( ! texturesection )
			return 0;
		texturesection->MakeTextureSection(this->textures[TextureId],++(this->idcounter),0.0,0.0,1.0,1.0);
		this->texturesections[this->idcounter] = texturesection;
		return this->idcounter;
	}
	return 0;
}

unsigned int TextureManager::AddTextureSection(unsigned int TextureId, float left, float top, float right, float bottom)
{
	if( this->textures.count( TextureId ) > 0 )
	{
		TextureSection* texturesection = new TextureSection();
		if( ! texturesection )
			return 0;
		texturesection->MakeTextureSection(this->textures[TextureId],++(this->idcounter),left,top,bottom,right);
		this->texturesections[this->idcounter] = texturesection;
		return this->idcounter;
	}
	return 0;
}

unsigned int TextureManager::CreateAnimation()
{
	Animation* animation = new Animation();
	if( ! animation )
		return 0;
	this->animations[++(this->idcounter)] = animation;
	return this->idcounter;
}

bool TextureManager::PushSectionToAnimation(unsigned int AnimationId, unsigned int SectionId, float delay)
{
	if(( this->texturesections.count( SectionId ) == 0 ) && ( this->textures.count( SectionId ) > 0 ))
	{
		SectionId = this->AddTextureSection( SectionId );
	}

	if( this->animations.count(AnimationId) > 0 )
	{
		return this->animations[AnimationId]->PushFrame(this->texturesections[ SectionId ], delay);
	}
	return false;
}

bool TextureManager::PushTextureToAnimation(unsigned int AnimationId, unsigned int TextureId, unsigned int NumberOfFrames, float delay)
{
	if( this->IsAnimation( AnimationId ) && this->IsTexture( TextureId ) )
	{
		for( unsigned int i = 0; i < NumberOfFrames; i++ )
		{
			int sectionid;
			if( sectionid = this->AddTextureSection( TextureId, i * (1.0 / NumberOfFrames), 0.0, (i + 1) * (1.0 / NumberOfFrames), 1.0 ) )
				this->PushSectionToAnimation( AnimationId, sectionid, delay);
			else
				return 0;
		}
	}
	else
		return 0;
	return 0;
}

bool TextureManager::Draw(unsigned int ElementId, int X, int Y, GLfloat Scale,GLfloat Rotation, GLfloat red, GLfloat green, GLfloat blue)
{
	if( this->IsAnimation( ElementId ) )
		return this->animations[ ElementId ]->DrawAnimation( X, Y, Scale, Rotation, red, green, blue);
	else if( this->IsTextureSection( ElementId ) )
		return this->texturesections[ ElementId ]->DrawTextureSection( X, Y, Scale, Rotation, red, green, blue);
	else if( this->IsTexture( ElementId ) )
		return this->textures[ ElementId ]->DrawTexture( X, Y, Scale, Rotation, red, green, blue);
	else
		return false;
	return false;
}

void TextureManager::UpdateAnimations()
{
	for( std::map<int, Animation*>::const_iterator i = this->animations.begin(); i != this->animations.end(); ++i )
		i->second->Update();
}

void TextureManager::PlayAnimations()
{
	for( std::map<int, Animation*>::const_iterator i = this->animations.begin(); i != this->animations.end(); ++i )
		i->second->Play();
}

void TextureManager::PauseAnimations()
{
	for( std::map<int, Animation*>::const_iterator i = this->animations.begin(); i != this->animations.end(); ++i )
		i->second->Pause();
}

void TextureManager::DeleteAnimations()
{
	for( std::map<int, Animation*>::const_iterator i = this->animations.begin(); i != this->animations.end(); ++i )
		delete i->second;
	this->animations.clear();
}

void TextureManager::DeleteTextureSections()
{
	this->DeleteAnimations();
	for( std::map<int, TextureSection*>::const_iterator i = this->texturesections.begin(); i != this->texturesections.end(); ++i )
		delete i->second;
	this->texturesections.clear();
}

void TextureManager::DeleteTextures()
{
	this->DeleteTextureSections();
	for( std::map<int, Texture*>::const_iterator i = this->textures.begin(); i != this->textures.end(); ++i )
		delete i->second;
	this->textures.clear();
}

